﻿using System;
using System.Collections.Generic;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS.Web;
using IFRS_Global;

public partial class IFRS_WIN05_4A : PageBase
{
    #region === Event ===

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewMenu();
            BindControl();
            BindData();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }
    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN05_1S.aspx");
    }
    protected void btnSAVE_NEW_Click(object sender, EventArgs e)
    {
        SaveData(true);
    }
    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl(Request.Url.AbsolutePath));
    }

    #endregion

    #region === Method ===

    private void BindControl()
    {
        btnSAVE.ValidationGroup = "Master";
        btnSAVE_NEW.ValidationGroup = btnSAVE.ValidationGroup;

        reqTransactionDate.SetRequiredField(lblTransactionDate, txtTransactionDate, btnSAVE);
        reqGLAccount.SetRequiredField(lblGLAccount, txtGLAccountCode, btnSAVE);
        reqCostCenter.SetRequiredField(lblCostCenter, txtCostCenter, btnSAVE);
        reqUnpaidAmount.SetRequiredField(lblUnpaidAmount, txtUnpaidAmount, btnSAVE);
        reqCurrencyCode.SetRequiredField(lblCurrencyCode, txtCurrencyCode, btnSAVE);
        reqCreditLevel.SetRequiredField(lblCreditLevel, ddlCreditLevel, btnSAVE);
        reqContractLoanType.SetRequiredField(lblContractLoanType, ddlContractLoanType, btnSAVE);
        reqLoanObjectiveID.SetRequiredField(lblLoanObjectiveID, txtLoanObjectiveID, btnSAVE);

        btnGLAccountCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/GLAccount.aspx") + "','700','550');");
        txtGLAccountCode.AddControl(txtGLAccountCode.ClientID);
        txtGLAccountCode.ControlSearch = txtGLAccountCode.ClientID;
        txtGLAccountCode.SearchKey = "GLAccount_oGLAccountCode_oGLAccountName";

        btnCostCenter.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/CostCenter.aspx") + "','700','550');");
        txtCostCenter.AddControl(hddCostCenter.ClientID);
        txtCostCenter.AddControl(txtCostCenter.ClientID);
        txtCostCenter.ControlSearch = txtCostCenter.ClientID;
        txtCostCenter.SearchKey = "VWCostCenter_oCOST_CENTER_CD_oCOST_CENTER_NM";

        btnLoanObjectiveID.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/VmLoanObjective.aspx") + "','700','550');");
        txtLoanObjectiveID.AddControl(hddLoanObjectiveID.ClientID);
        txtLoanObjectiveID.AddControl(txtLoanObjectiveID.ClientID);
        txtLoanObjectiveID.ControlSearch = txtLoanObjectiveID.ClientID;
        txtLoanObjectiveID.SearchKey = "VmLoanObjective_oCBT_CUST_BUSINESS_TYPE_ID_oCBT_DESC_LEV1";

        btnCurrencyCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/Currency.aspx") + "','700','550');");
        txtCurrencyCode.AddControl(txtCurrencyCode.ClientID);
        txtCurrencyCode.ControlSearch = txtCurrencyCode.ClientID;
        txtCurrencyCode.SearchKey = "Currency_oCurrencyCode_oCurrencyName";
        txtCurrencyCode.Text = DefaultValue.CurrencyCode.THB;

        ddlCreditLevel.SetData(DDL_TYPE.CreditLevel, string.Empty, false, true);
        ddlContractLoanType.SetData(DDL_TYPE.ContractLoanType, string.Empty, false, true);

        txtStatus.Text = Resources.ifrsField.CREATE;
    }

    private void BindData()
    {
        if (Convert.ToString(Request.QueryString["id"]) != string.Empty)
        {
            
            var id = Convert.ToInt32(Request.QueryString["id"]);
            var dt = new SPR_NOID().SEARCH_BYID(id);
            if (dt != null && dt.Rows.Count > 0)
            {
                var list = new List<NOID>();
                ConvertHelper.ConvertDataTableToObject(dt, list);

                txtTransactionDate.Text = DateTime.Now.DateToString();

                txtGLAccountCode.Text = list[0].GLAccount;
                hddCostCenter.Value = list[0].CostCenter;
                txtCostCenter.Text = list[0].COST_CENTER_NM;
                txtUnpaidAmount.Text = list[0].UnpaidAmount.DecimalToString();
                txtCurrencyCode.Text = list[0].CurrencyCode;
                ddlCreditLevel.SelectedValue = list[0].CreditLevelID.IntToString();
                hddLoanObjectiveID.Value = list[0].LoanObjectiveID;
                txtLoanObjectiveID.Text = list[0].CBT_DESC_LEV1;
                ddlContractLoanType.SelectedValue = list[0].ContractLoanTypeID.IntToString();
                txtReservedAmount.Text = list[0].ReservedAmount.DecimalToString();
            }
        }
    }

    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN05_4A;
    }
    private void SaveData(bool saveNew = false)
    {
        if (!Page.IsValid) return;

        var dal = new SPR_NOID();
        var models = new NOID();

        models.TransactionDate = txtTransactionDate.Text.StringToDateAllowNull();

        models.GLAccount = txtGLAccountCode.Text;
        models.CostCenter = hddCostCenter.Value;
        models.UnpaidAmount = txtUnpaidAmount.Text.AsDecimal();
        models.CurrencyCode = txtCurrencyCode.Text;
        models.CreditLevelID = ddlCreditLevel.SelectedValue.AsInt32();
        models.LoanObjectiveID = hddLoanObjectiveID.Value;
        models.ContractLoanTypeID = ddlContractLoanType.SelectedValue.AsInt32();
        models.ReservedAmount = txtReservedAmount.Text.AsDecimal();

        models.IsConfirmed = false;

        models.RP_GSB_PRDCD = DefaultValue.RP_GSB_PRDCD.MANUAL_NOID;
        models.RP_SOURCE_FILE = DefaultValue.RP_SOURCE_FILE.MANUAL_NOID;
        models.RP_MAPPING_CD = DefaultValue.RP_MAPPING_CD.MANUAL_NOID;

        models.SetStandardField();

        var id = dal.INSERT(models);
        if(id > 0)
        {
            if (saveNew)
                JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(Request.Url.AbsolutePath));
            else
                JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN05_2E.aspx?&id={0}", id)));
        }
        else // Duplicate
            Utility.AlertMessage(Page, id == -1 ? Resources.ifrsResource.Duplicate : Resources.ifrsResource.CanNotSaveData);
    }

    #endregion
}